package com.yuong.demo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.yuong.demo.DensityUtil;

import androidx.annotation.Nullable;

/**
 * 〈〉
 *
 * @author yuandong
 * @create 2021/4/26
 * @since 1.0.0
 */
public class RoseView extends View {
    private int width;
    private int height;
    private float centerX, centerY;

    private float radius;
    private float paddingTop;
    private float paddingBottom;
    private float outCircleWidth;
    private float outCircleRadius;
    private float innerCircleRadius;
    private float lineWidth;
    private float valueTextSize;
    private float yOffset;
    private float leftX, rightX;

    private Paint paint;
    private Paint outCirclePaint;
    private Paint innerCirclePaint;
    private Paint linePaint;
    private Paint valuePaint;
    private Paint.FontMetrics valueFontMetrics;

    private float[] ratios = {0.5f, 0.6f, 0.7f, 0.8f, 1.0f, 0.8f};
    private float[] angles = {60, 60, 60, 60, 60, 60};
    private int[] colors = {Color.RED, Color.GREEN, Color.YELLOW, Color.GRAY, Color.BLUE, Color.BLACK};

    private float lastAngle;

    public RoseView(Context context) {
        this(context, null);
    }

    public RoseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paddingTop = DensityUtil.dp2px(getContext(), 50);
        paddingBottom = DensityUtil.dp2px(getContext(), 50);
        outCircleWidth = DensityUtil.dp2px(getContext(), 12);
        outCircleRadius = DensityUtil.dp2px(getContext(), 25);
        innerCircleRadius = DensityUtil.dp2px(getContext(), 10);
        lineWidth = DensityUtil.dp2px(getContext(), 0.5f);
        valueTextSize = DensityUtil.sp2px(getContext(), 12);
        yOffset = DensityUtil.dp2px(getContext(), 27);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);

        outCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outCirclePaint.setStyle(Paint.Style.STROKE);
        outCirclePaint.setStrokeWidth(outCircleWidth);
        outCirclePaint.setColor(Color.WHITE);

        innerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        innerCirclePaint.setStyle(Paint.Style.FILL);
        innerCirclePaint.setColor(Color.WHITE);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setColor(Color.BLACK);
        linePaint.setStrokeWidth(lineWidth);

        valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        valuePaint.setStyle(Paint.Style.FILL);
        valuePaint.setColor(Color.parseColor("#666666"));
        valuePaint.setTextSize(valueTextSize);
        valueFontMetrics = valuePaint.getFontMetrics();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        radius = width * 0.5f * 0.5f;//直径为view宽度的0.5
        centerX = 0.5f * width;//圆心x
        centerY = paddingTop + radius;
        leftX = radius * 0.5f;
        rightX = width - radius * 0.5f;
        height = (int) (width * 0.5f + paddingTop + paddingBottom);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawArc(canvas);
        drawCircle(canvas);
        drawLine(canvas);
    }

    private void drawArc(Canvas canvas) {
        canvas.save();
        canvas.rotate(-90, centerX, centerY);//以圆心旋转的
        for (int i = 0; i < ratios.length; i++) {
            drawArc(canvas, ratios[i], angles[i], colors[i]);
        }
        canvas.restore();
    }

    private void drawArc(Canvas canvas, float ratio, float angle, int color) {
        float left = centerX - radius * ratio;
        float right = centerX + radius * ratio;
        float top = centerY - radius * ratio;
        float bottom = centerY + radius * ratio;

        Log.e("*****", "lastAngle : " + lastAngle);
        RectF oval = new RectF(left, top, right, bottom);
        paint.setColor(color);
        canvas.drawArc(oval, lastAngle, angle, true, paint);
        lastAngle += angle;
    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(centerX, centerY, outCircleRadius - outCircleWidth * 0.5f, outCirclePaint);
        canvas.drawCircle(centerX, centerY, innerCircleRadius, innerCirclePaint);
    }

    private void drawLine(Canvas canvas) {
        float angle = 0;
        for (int i = 0; i < ratios.length; i++) {
            angle += angles[i];
            drawLine(canvas, ratios[i], angle - angles[i] * 0.5f);
        }
    }

    private void drawLine(Canvas canvas, float ratio, float angle) {
        double pi = Math.PI * angle / 180d;
        float x = (float) (centerX + Math.sin(pi) * radius * ratio);
        float y = (float) (centerY - Math.cos(pi) * radius * ratio);
        String text = "27%";
        valuePaint.measureText(text);
        if (pi < Math.PI * 0.25f) {//0-45
            float x1 = x + radius * ratio * 0.25f;
            float y1 = y - yOffset;
            valuePaint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawLine(x - radius * ratio * 0.25f, y, x1, y1, linePaint);
//            canvas.drawLine(x1, y1, x1 + radius * ratio, y1, linePaint);
//            canvas.drawText(text, x1 + radius * ratio, y1 - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x1, y1, rightX, y1, linePaint);
            canvas.drawText(text, rightX, y1 - valueFontMetrics.bottom, valuePaint);
        } else if (pi >= Math.PI * 0.25f && pi <= Math.PI * 0.75f) {//45-135
            valuePaint.setTextAlign(Paint.Align.RIGHT);
//            canvas.drawLine(x - radius * ratio * 0.25f, y, x + radius * ratio, y, linePaint);
//            canvas.drawText(text, x + radius * ratio, y - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x - radius * ratio * 0.25f, y, rightX, y, linePaint);
            canvas.drawText(text, rightX, y - valueFontMetrics.bottom, valuePaint);
        } else if (pi > Math.PI * 0.75f && pi <= Math.PI) {//135-180
            valuePaint.setTextAlign(Paint.Align.RIGHT);
            float x1 = x + radius * ratio * 0.25f;
            float y1 = y + yOffset;
            canvas.drawLine(x - radius * ratio * 0.25f, y, x1, y1, linePaint);
//            canvas.drawLine(x1, y1, x1 + radius * ratio, y1, linePaint);
//            canvas.drawText(text, x1 + radius * ratio, y1 - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x1, y1, rightX, y1, linePaint);
            canvas.drawText(text, rightX, y1 - valueFontMetrics.bottom, valuePaint);
        } else if (pi > Math.PI && pi < Math.PI * 1.25f) {//180-225
            valuePaint.setTextAlign(Paint.Align.LEFT);
            float x1 = x - radius * ratio * 0.25f;
            float y1 = y + yOffset;
            canvas.drawLine(x + radius * ratio * 0.25f, y, x1, y1, linePaint);
//            canvas.drawLine(x1, y1, x1 - radius * ratio, y1, linePaint);
//            canvas.drawText(text, x1 - radius * ratio, y1 - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x1, y1, leftX, y1, linePaint);
            canvas.drawText(text, leftX, y1 - valueFontMetrics.bottom, valuePaint);
        } else if (pi >= Math.PI * 1.25f && pi <= Math.PI * 1.75f) {//225-315
            valuePaint.setTextAlign(Paint.Align.LEFT);
//            canvas.drawLine(x + radius * ratio * 0.25f, y, x - radius * ratio, y, linePaint);
//            canvas.drawText(text, x - radius * ratio, y - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x + radius * ratio * 0.25f, y, leftX, y, linePaint);
            canvas.drawText(text, leftX, y - valueFontMetrics.bottom, valuePaint);
        } else if (pi > Math.PI * 1.75f) {//315-360
            valuePaint.setTextAlign(Paint.Align.LEFT);
            float x1 = x - radius * ratio * 0.25f;
            float y1 = y - yOffset;
            canvas.drawLine(x + radius * ratio * 0.25f, y, x1, y1, linePaint);
//            canvas.drawLine(x1, y1, x1 - radius * ratio, y1, linePaint);
//            canvas.drawText(text, x1 - radius * ratio, y1 - valueFontMetrics.bottom, valuePaint);
            canvas.drawLine(x1, y1, leftX, y1, linePaint);
            canvas.drawText(text, leftX, y1 - valueFontMetrics.bottom, valuePaint);
        }
    }
}